package com.css.fxfzcpxx.modules.serviceCase.service.impl;

import com.css.fxfzcpxx.base.attachment.service.AttachmentInfoService;

import com.css.fxfzcpxx.common.utils.UpdateUtil;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.informationDelivery.vo.InfDeliveryVo;
import com.css.fxfzcpxx.modules.serviceCase.entity.ServiceCase;
import com.css.fxfzcpxx.modules.serviceCase.param.ClaCasParams;
import com.css.fxfzcpxx.modules.serviceCase.param.QueParams;
import com.css.fxfzcpxx.modules.serviceCase.repository.ServiceCaseRepository;
import com.css.fxfzcpxx.modules.serviceCase.repository.impl.ServiceCaseRepositoryImpl;
import com.css.fxfzcpxx.modules.serviceCase.service.ServiceCaseService;
import com.css.fxfzcpxx.modules.serviceCase.vo.ListVo;
import com.css.fxfzcpxx.modules.serviceCase.vo.ServiceCaseVo;
import com.css.fxfzcpxx.util.EasyPoiExcelUtil;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023/9/19 15:09
 * @Description:
 */
@Service
public class ServiceCaseServiceImpl implements ServiceCaseService {
    @Resource
    ServiceCaseRepository serviceCaseRepository;

    @Resource
    AttachmentInfoService attachmentInfoService;

    @Override
    public Map<String, Object> getPage(QueParams queParams, int curPage, int pageSize) {
        Map<String, Object> page = serviceCaseRepository.getPage(queParams,curPage, pageSize);
        return page;
    }

    @Override
    public void save(ServiceCase serviceCase) {
        serviceCase.setId(UUIDGenerator.getUUID());
        serviceCase.setCreateUser(PlatformSessionContext.getUserID());
        serviceCase.setCreateTime(new Date());
        serviceCase.setDelFlag(YNEnum.N.toString());
        serviceCase.setSign(YNEnum.N.toString());
        serviceCase.setSort(0);
        serviceCase.setReleaseStatus(YNEnum.Y.toString());
        serviceCaseRepository.save(serviceCase);
    }

    @Override
    public void recallOrPublish(QueParams queParams) {
        serviceCaseRepository.recallOrPublish(queParams.getReleaseStatus(), Arrays.asList(queParams.getIds().split(",")));
    }

    @Override
    public void delete(String ids) {
        serviceCaseRepository.delete(Arrays.asList(ids.split(",")));
    }

    @Override
    public ServiceCaseVo findById(String id) {
        ServiceCaseVo serviceCaseVo=new ServiceCaseVo();
        ServiceCase serviceCase=serviceCaseRepository.findById(id);
        UpdateUtil.copyNullProperties(serviceCase, serviceCaseVo);
        if (PlatformObjectUtils.isNotEmpty(serviceCase.getMeansUpload())){
            serviceCaseVo.setMeansUploadAtt(attachmentInfoService.queryFile(serviceCase.getMeansUpload()));
        }

        return serviceCaseVo;
    }

    @Override
    public void update(ServiceCase serviceCase) {
        serviceCase.setUpdateTime(new Date());
        serviceCase.setUpdateUser(PlatformSessionContext.getUserID());

        if("0".equals(serviceCase.getReleaseStatus())){
            serviceCaseRepository.publishedDataupdate(serviceCase);
        }else{
            serviceCaseRepository.unpublishedDataupdate(serviceCase);
        }
    }

    @Override
    public void catalogDownload(HttpServletResponse response, String ids) {
        List<ServiceCase> infDelivery = serviceCaseRepository.getDataById(Arrays.asList(ids.split(",")));
        if (infDelivery.size() <= 0) {
            throw new RuntimeException("数据为空,无法导出！");
        }
        try {
            EasyPoiExcelUtil.exportExcel(infDelivery, "服务案例数据信息统计", "服务案例数据信息", ServiceCase.class, "服务案例数据信息", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ServiceCase> getMoveData(QueParams params) {
        return serviceCaseRepository.getMoveData(params);
    }

    @Override
    public void meansTypeUpdate(ListVo listvo) {
        List<ServiceCase> list = listvo.getList();
        for(ServiceCase item:list){
            serviceCaseRepository.meansTypeUpdate(item.getId(),item.getMeansTypeId());
        }
    }

    @Override
    public Map<String, Object> getClaCasPage(ClaCasParams claCasParams, int curPage, int pageSize) {
        Map<String, Object> page = serviceCaseRepository.getClaCasPage(claCasParams,curPage, pageSize);
        List<ServiceCaseVo> rows = (List<ServiceCaseVo>) page.get("rows");
        for(ServiceCaseVo item:rows){
            if (PlatformObjectUtils.isNotEmpty(item.getMeansUpload())){
                item.setMeansUploadAtt(attachmentInfoService.queryFile(item.getMeansUpload()));
            }
        }
        return page;
    }

    @Override
    public List<ServiceCase> getLatestCase() {
        return serviceCaseRepository.getLatestCase();
    }

    @Override
    public List<ServiceCaseVo> getRotationChartData() {
        List<ServiceCaseVo> rotationChartData = serviceCaseRepository.getRotationChartData();
        for(ServiceCaseVo item:rotationChartData){
            if (PlatformObjectUtils.isNotEmpty(item.getMeansUpload())){
                item.setMeansUploadAtt(attachmentInfoService.queryFile(item.getMeansUpload()));
            }
        }
        return rotationChartData;
    }

    @Override
    public void setRotationChart(List<String> ids) {
        for (int i = 0; i < ids.size(); i++) {
            serviceCaseRepository.setRotationChart(ids.get(i),i,YNEnum.Y.toString());
        }
    }

    @Override
    public void cancelRotationChart(List<String> ids) {
        serviceCaseRepository.cancelRotationChart(ids,0,YNEnum.N.toString());
    }
}
